package com.liqy.api.controller;

import com.alibaba.fastjson.JSON;
import com.liqy.api.cache.RouterCache;
import com.liqy.api.domain.Response;
import com.liqy.api.domain.RouterInfo;
import com.liqy.api.util.GroovyScriptFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * 脚本内存管理
 */
@Slf4j
@RestController
@RequestMapping("api")
public class RouterCacheController {

    @Value("${apiDemo.api.context}")
    String apiContext;

    @Autowired
    RouterCache cacheManager;

    @Autowired
    GroovyScriptFactory groovyScriptFactory;

    /*@Autowired
    RouterConfigService routerConfigService;*/

    @RequestMapping("/rm")
    public Response cleanCache(@RequestParam("url") String key) {
        log.info(String.format("api cache clean: %s", key));
        Object remove = cacheManager.remove(key);
//        routerConfigService.delete(key);
        groovyScriptFactory.rmScript(String.valueOf(remove));
        return Response.successResponse();
    }

    @RequestMapping("/add")
    public Response addCache(RouterInfo routerInfo) {
        log.info(String.format("api cache add: %s", JSON.toJSONString(routerInfo)));
        if (Objects.nonNull(routerInfo) && Objects.nonNull(routerInfo.getUrl()) && Objects.nonNull(routerInfo.getScript())) {
            cacheManager.put(routerInfo.getUrl(), routerInfo.getScript());
//            routerConfigService.add(routerInfo);
            groovyScriptFactory.setScript(routerInfo.getScript());
        } else {
            return Response.errResponse(String.format("请检查参数：%d", routerInfo));
        }
        return Response.successResponse();
    }

    @PostMapping("/add_all")
    public Response addAllCache(@RequestBody Map map) {
        log.info(String.format("api cache addAll: %s", JSON.toJSONString(map)));
        if (Objects.nonNull(map)) {
            cacheManager.putAll(map);
//            List<RouterInfo> collect = (List<RouterInfo>) map.keySet().stream().map(current -> RouterInfo.builder().url(String.valueOf(current)).script(String.valueOf(map.get(current))).build()).collect(Collectors.toList());
//            routerConfigService.addAll(collect);
            map.forEach((k, v) -> {
                groovyScriptFactory.setScript(String.valueOf(v));
            });
        } else {
            return Response.errResponse(String.format("请检查参数：%d", map));
        }
        return Response.successResponse();
    }

    @PostMapping("/set")
    public Response updateCache(@RequestBody RouterInfo routerInfo) {
        log.info(String.format("api cache update: %s", JSON.toJSONString(routerInfo)));
        if (cacheManager.containsKey(routerInfo.getUrl())) {
            groovyScriptFactory.rmScript(routerInfo.getScript());
        }
        cacheManager.put(routerInfo.getUrl(), routerInfo.getScript());
//        routerConfigService.update(routerInfo);
        groovyScriptFactory.setScript(routerInfo.getScript());
        return Response.successResponse();
    }

    @RequestMapping("/view")
    public Response viewCache(@RequestParam("url") String key) {
        log.info(String.format("api cache view one: %s", key));
        Object o = cacheManager.get(key);
        return Response.successResponse(o);
    }

    @RequestMapping("/view_all")
    public Response viewAllCache() {
        log.info("api cache view all");
        return Response.successResponse(cacheManager.getRoutersCache());
    }

}
